﻿using Sprout.Core.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Sprout.Repository
{
    /// <summary>
    /// 用于控制全局事务
    /// </summary>
    internal sealed class TransactionService
    {
        private static volatile TransactionService _transactionService;
        private static readonly object _locker = new object();


        /// <summary>
        /// 实例
        /// </summary>
        public static TransactionService Instance
        {
            get
            {
                if (_transactionService == null)
                {
                    lock (_locker)
                    {
                        if (_transactionService == null)
                        {
                            _transactionService = new TransactionService();
                        }
                    }
                }
                return _transactionService;
            }
        }

        /// <summary>
        /// 上下文名称
        /// </summary>
        string transContext = "transaction_session";

        /// <summary>
        /// 是否开启事务
        /// </summary>
        /// <returns></returns>
        public bool IsTransaction()
        {
            var transactionProcess = CallContext.GetData(transContext);
            if (transactionProcess == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 获取或设置事务
        /// </summary>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public TransactionMap GetOrAddTransaction(string dataSource)
        {
            var transactionProcess = CallContext.GetData(transContext) as TransactionProcess;
            if (transactionProcess == null)
            {
                throw new Exception("未开启事务");
            }
            return transactionProcess.GetOrAddTransaction(dataSource);
        }
        /// <summary>
        /// 开启全局事务
        /// </summary>
        /// <returns></returns>
        public void BeginTransaction()
        {
            var transactionProcess = CallContext.GetData(transContext) as TransactionProcess;
            if (transactionProcess != null)
            {
                throw new Exception("事务处理出现了非常规异常");
            }
            transactionProcess = new TransactionProcess();
            CallContext.SetData(transContext, transactionProcess);
        }
        /// <summary>
        /// 提交全局事务
        /// </summary>
        public void Commit()
        {
            var transactionProcess = CallContext.GetData(transContext) as TransactionProcess;
            if (transactionProcess == null)
            {
                throw new Exception("未开启事务");
            }
            transactionProcess.Commit();

            CallContext.SetData(transContext, null);
        }
        /// <summary>
        /// 回滚全局事务
        /// </summary>
        public void Rollback()
        {
            var transactionProcess = CallContext.GetData(transContext) as TransactionProcess;
            if (transactionProcess == null)
            {
                throw new Exception("未开启事务");
            }
            transactionProcess.Rollback();

            CallContext.SetData(transContext, null);
        }
    }
}
